<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('数据包生成列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">


                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('wenyanwen:generate:edit')}]];
        var removeFlag = [[${@permission.hasPermi('wenyanwen:generate:remove')}]];
        var dbNameDatas = [[${@dict.getType('db_generate_db_name')}]];
        var prefix = ctx + "wenyanwen/generate";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "数据包生成",
                columns: [{
                    checkbox: true
                },
                {
                    field : 'dbName', 
                    title : '数据包'
                },
                {
                    field : 'desc', 
                    title : '名称'
                },
                {
                    field : 'status', 
                    title : '状态',
                    formatter: function(value, row, index){
                        return "<span id='status_" + row.id +"'>"
                    }
                },
                {
                    field : 'progress', 
                    title : '当前进度',
                    width: 300,
                    formatter: function(value, row, index){
                        return "<div class='progress' style='margin-bottom: 0' id='progress_" + row.id + "'></div>";
                    }
                },
                {
                    field : 'lastTime', 
                    title : '最后生成时间'
                },
                {
                    field : 'app',
                    title : '应用'
                },
                {
                    field : 'resName',
                    title : '资源名称'
                },
                {
                    field : 'version',
                    title : '版本'
                },
                {
                    field : 'supportVersion',
                    title : 'supportVersion'
                },
                {
                    field : 'lastResult', 
                    title : '最后生成结果',
                    formatter: function(value, row, index){
                        return "<span id='result_" + row.id +"'>"
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {

                        setInterval( () => {

                           var config = {
                                url: prefix + "/progress/" + row.id,
                                type: "get",
                                dataType: "json",
                                data: "",
                                beforeSend: function () {

                                },
                                success: function(result) {
                                    var data = result.data;
                                    $("#progress_" + row.id).html(data.progress);
                                    $("#result_" + row.id).html(data.result);
                                    $("#status_" + row.id).html(data.status);
                                    if(data.st != 1 && data.st != 2){
                                       $("#generate_" + row.id).show();
                                    }else{
                                       $("#generate_" + row.id).hide();
                                    }

                                    if(data.st == 2){
                                       $("#stop_" + row.id).show();
                                    }else{
                                       $("#stop_" + row.id).hide();
                                    }
                                }
                            };
                            $.ajax(config)

                        }, 2000)

                        var actions = [];
                        actions.push('<a style="" class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a style="display:none" id="generate_' + row.id + '" class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="start(\'' + row.dbName + '\')"><i class="fa fa-edit"></i>生成</a> ');
                        actions.push('<a style="display:none" id="stop_' + row.id + '" class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="stop(\'' + row.dbName + '\')"><i class="fa fa-remove"></i>停止</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        function start(dbName){
            $.modal.confirm('确定要生成数据包[' + dbName + ']吗?', function() {
				$.operate.post(prefix + "/addTask", { "dbName": dbName, "command": "GENERATE"});
		    })
        }

        function stop(dbName){
            $.modal.confirm('确定要取消生成数据包[' + dbName + ']吗?', function() {
				$.operate.post(prefix + "/addTask", { "dbName": dbName, "command": "STOP"});
		    })
        }

</script>
</body>
</html>